<?php

namespace XtwsBack\Controller;

use Think\Controller;

use think\Validate;



class StoreSalerController extends CommonController {



    // 门店店员列表

    public function index(){

        $saler = M('shop_saler');
		$where="";
		if($_GET['keyword']!=''){
			$where=" and c.storename like '%".$_GET['keyword']."%'";
			$this->assign('keyword',$_GET['keyword']);// 赋值分页输出
		}
        $count = $saler->where('status <> -1')->count();// 查询满足要求的总记录数

        $Page = new \Think\Page($count,100);// 实例化分页类 传入总记录数和每页显示的记录数(25),查出一百条记录到前台，再由前台js处理成分页

        $show = $Page->show();// 分页显示输出

        $list = $saler->table(C('DB_PREFIX')."shop_saler as b")

                ->join(C('DB_PREFIX')."member as a ON a.id=b.u_id")

                ->join(C('DB_PREFIX')."shop_store as c ON c.id=b.storeid")

                ->field('b.*,a.nickname,c.storename')

                ->where('b.status <> -1'.$where)

                ->order('id desc')

                ->limit($Page->firstRow.','.$Page->listRows)

                ->select();

        $this -> assign('list',$list);

        $this -> assign('page',$show);

        //加载视图

        $this->display();

    }//end fun





    // 添加修改店员信息

    public function addSaler(){

        $saler = M('shop_saler');

        $id  = intval(I('id'));

        if(IS_POST){

            $data = I('post.') ;

            if(empty($data['u_id'])){

                $this -> error('请选择会员');

            }

            if(empty($data['storeid'])){

                $this -> error('请选择所属门店');

            }

            if($id){//编辑

                $res = $saler -> save($data);

            }else{//添加

                $res = $saler -> add($data);

            }// end if

            if($res){

                $this -> success('提交成功','http://www.xiangtws.com/XtwsBack/StoreSaler/index');

            }else{

                $this -> error('提交失败');

            }// end if

        }else{

            if($id){

                $data['salerInfo'] = $saler->where(array('id'=>$id))->find();

            }else{

                $data['salerInfo'] = 1;

            }

            $data['member'] = M('member')->where('status <> -1')->field('id,nickname')->select();    //查询所有会员信息

            $data['shop_store'] = M('shop_store')->where('status <> -1')->field('id,storename')->select();     //查询所有门店信息

            $this -> assign($data);

            $this -> display();

        }// end if



    }// end fun





    // 删除会员

    public function del_saler(){

        $id  = intval(I('id'));

        if($id){

            $saler = M('shop_saler');

            $data['status'] = -1;

            $data['id'] = $id;

            $res = $saler->save($data);

            if($res){

                $this->success('删除成功');

            }else{

                $this->error('出错啦，请检查');

            }

        }else{

            $this->error('请先选择数据');

        }



    }//end fun



    // 推送模板消息

    public function sendMsg(){

        dump('111');

//        $a = sendMsg('1269');

    } //end fun





}